<template>
    <div class="login_container">
      <div class="beg"></div>
      <div class="login_box">
        <!--头像-->
        <div class="avatar_box">
          <img src="../assets/logo.png">
        </div>
        <!--表单-->
        <el-form :model="loginForm" ref="loginFormRef" status-icon class="demo-ruleForm" :rules="rules">
          <el-form-item prop="username">
            <el-input type="text" v-model="loginForm.username" prefix-icon="iconfont icon-user" autocomplete="off" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" autocomplete="off" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="login">登陆</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
</template>

<script>
  import connector from '../base/interface.js';
    export default {
      data(){
        return{
          loginForm:{username:'',password:''},
          /*表单验证规则*/
          rules:{
            username:[{ required: true, message: '请输入用户名', trigger: 'blur' },{min:3,max:12, message: '长度在 3 到 12 个字符', trigger: 'blur' }],
            password: [{ required: true, message: '请输入密码', trigger: 'blur' },{min:3,max:12, message: '长度在 3 到 12 个字符', trigger: 'blur' }]
          }
        }
      },
      methods:{
        reset(){
          /*重置表单*/
          this.$refs.loginFormRef.resetFields();
        },
        login(){
          this.$refs.loginFormRef.validate(vaild=>{
            if(!vaild)return;
            /*发起登陆请求*/
            connector.login(this.loginForm).then(res=>{
              const {data:data}=res;
              if(data.meta.status != 200){
                this.$message.error({ message:data.meta.msg});
                return;
              }
              /*将token保存到 seesionStorage 中*/
              this.$message({ message:data.meta.msg,type: 'success'});
              window.sessionStorage.setItem('token',data.data.token);
              this.$router.push('/home');
            },err=>{
              this.$message.err({ message:data.meta.msg});
            })
          })
        }
      }
    }
</script>

<style scoped lang="scss">
  @import "../assets/css/public.scss";
  .login_container{
  background: #2A4B6A;
  height: 100%;
    .beg{
      @include imgFull;
      @include bgc('../assets/images/beg.jpg');
      -webkit-filter: blur(15px);
      -moz-filter: blur(15px);
      -o-filter: blur(15px);
      -ms-filter: blur(15px);
      filter: blur(15px);
    }
  .login_box{
    width: 500px;
    height: 300px;
    background-color:rgba(0,0,0,.6);
    border-radius: 5px;
    @include centered;
    .avatar_box{
      width: 130px;
      height: 130px;
      border: 2px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%,-50%);
      background-color: white;
      img{
        @include imgFull;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
  .demo-ruleForm{
    position: absolute;
    bottom: 10px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    .btns{
      display: flex;
      justify-content: flex-end;
    }

  }
}
</style>
